﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DepSoft.Helpers.iTunes.Elements;
using System.IO;

namespace DepSoft.Helpers.iTunes.Writers
{
    public class M3UExtPlaylistWriter : PlaylistWriter
    {
        #region Private members
        private const string FILE_HEADER        = "#EXTM3U";
        private const string LINE_FORMAT_STRING = "{0}{1}, {2} - {3}";
        private const string LINE_PREFIX        = "#EXTINF:";

        private StreamWriter _writer;
        #endregion

        #region Constructor
        public M3UExtPlaylistWriter(string fileName, iTunesPlaylist playList)
            : base(fileName, playList)
        {
            this._writer = new StreamWriter(this.FileName, false, Encoding.UTF8);
            this._writer.WriteLine(FILE_HEADER);
        }
        #endregion

        #region PlaylistWriter implementation
        public override void WriteTrack(string trackName, iTunesTrack track)
        {
            //Each track has two lines in the playlist file
            //The first contains display info (seconds, name), plus the path/filename
            _writer.WriteLine(string.Format(LINE_FORMAT_STRING, LINE_PREFIX, track.TotalTime.ToString(), track.Artist, track.Name));
            _writer.WriteLine(trackName);
        }

        public override void Close()
        {
            this._writer.Close();
            this._writer.Dispose();
        }
        #endregion
    }
}
